/*
 * Copyright 2008 Jeff Dwyer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.rpi.bt.client.domain.commands;

import java.io.Serializable;

import com.allen_sauer.gwt.log.client.Log;
import com.apress.progwt.client.domain.commands.AbstractCommand;
import com.apress.progwt.client.domain.commands.CommandService;
import com.apress.progwt.client.exception.SiteException;

import edu.rpi.bt.client.domain.Destination;

/**
 * Because this will reference many objects that are stored in the DB, we
 * need to do loads on all of those objects in order to ensure that we
 * don't get 'multiple obejcts in the session' errors from hibernate.
 * 
 * Take care of those loads using the AbstractCommand pattern that we've
 * developed.
 * 
 * If it's an edit, we need to be sure to copy over all important,
 * changable fields to the toSave object which we'll retrieve from the DB.
 * 
 * @author Jeff Dwyer
 * 
 */
public class SaveDestinationCommand extends AbstractCommand implements
        Serializable {

    //private Destination trip;
    //private Destination location;

    private long destinationID;
    private long authorID;
    private Destination original;

    private Destination toSave;
    private long savedDestinationID;

    public SaveDestinationCommand() {
    }

    public SaveDestinationCommand(Destination destination) {
        super(destination);
        this.destinationID = destination.getId();
        original = destination;
        System.out.println("Name in constr :" +  destination.getName());
      
    }

    @Override
    public String toString() {
        return "SaveDestinationCommand Destination " + destinationID;
    }

    public boolean haveYouSecuredYourselfAndFilteredUserInput() {
        return true;
    }

    public void execute(CommandService commandService)
            throws SiteException {
System.out.println("In execute");
   //   User author = commandService.get(User.class, authorID);
      
//User author = commandService.get(User.class, 1);
      System.out.println("In execute 2...");
      toSave=original;
    //  toSave = commandService.get(Destination.class, destinationID);
      
      System.out.println("In execute 2a ");
      System.out.println("Name :" +  toSave.getName() + " user " + toSave.getUser());
//toSave.setUser(author);
      commandService.assertUserIsAuthenticated(toSave.getUser());
      System.out.println("In execute 2b ");
      Log.debug("SaveDestinationCommand " + destinationID + " loaded: "
              + toSave + " original: " + original);

    //  String xssFiltered = commandService.filterHTML(original.getDescription());

      //toSave.setNotes(xssFiltered);

      System.out.println("In execute 3");
      commandService.save(toSave);
      this.savedDestinationID = toSave.getId() ;
      System.out.println("In execute 4");
    }
    
    public long getSavedDestinationID() {
        return savedDestinationID;
    }
    /**
     * used in testing accessor
     * 
     * @return
     */
    public Destination getToSave() {
        return toSave;
    }

}
